<?php

namespace App\Http\Controllers\Api\Common;

use App\Facades\AirportsGeoFacade;
use App\Facades\StationsGeoFacade;
use App\Http\Controllers\Controller;
use App\Http\Resources\Common\Area\ProvincesResource;
use App\Http\Resources\Plane\LocationAirportResource;
use App\Http\Resources\Train\StationResource;
use App\Models\Common\Area\Repositories\Interfaces\AreaRepositoryInterface;
use App\Models\Plane\Airports\Repositories\Interfaces\AirportRepositoryInterface;
use App\Models\Train\Stations\Repositories\Interfaces\StationRepositoryInterface;
use App\Trip\Geo\Amap\POI;
use App\Trip\Geo\Location;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request;

class AreaController extends Controller
{

    private AreaRepositoryInterface    $areaRepo;
    private StationRepositoryInterface $stationRepo;
    private AirportRepositoryInterface $airportRepo;

    public function __construct(AreaRepositoryInterface    $areaRepository,
                                StationRepositoryInterface $stationRepository,
                                AirportRepositoryInterface $airportRepository,
    )
    {
        $this->areaRepo    = $areaRepository;
        $this->stationRepo = $stationRepository;
        $this->airportRepo = $airportRepository;
    }
    // 经纬度 获取当前位置

    /**
     * @throws GuzzleException
     * @throws Exception
     */
    public function location(Request $request)
    {
        $geo            = new POI();
        $input_location = new Location($request->input('location'));
        $types          = [
            '050000',
            '060000',
            '070000',
            '090000',
            '100000',
            '110000',
            '150105',
            '150203',
            '190100',
            '190300',
            '190400',
            '120000',
        ];
        $res            = $geo->around($input_location, implode('|', $types));
        $ret            = $res[0] ?? [];
        if (empty($ret)) {
            return $this->error(404, '暂无位置信息');
        }
        // 高德geo 返回 地理定位 citycode 为区号
        $city = $this->areaRepo->findCityByCode($ret['citycode']);
        $area = $this->areaRepo->findByCode($ret['adcode']);


        // LBS 定位 获取最近的火车站
        // 查询最近的一个火车站
        $station_code = StationsGeoFacade::nearestCode($input_location->getLongitude(), $input_location->getLatitude(), 50, 'km');
        // 如果获取到火车站
        $station   = $station_code ? $this->stationRepo->findOneByCode($station_code) : null;
        $area_name = $station ? $station->adname : $area->getAreaName();
        $iscity    = isUseCity($area_name);
        // 查询最近的一个机场
        $airport_code = AirportsGeoFacade::nearestCode($input_location->getLongitude(), $input_location->getLatitude(), 50, 'km');
        $airport      = $airport_code ? $this->airportRepo->findOneByCode($airport_code) : null;

        return $this->success('', [
            'citycode'    => $city->getAdCode(),
            'cityname'    => $city->getAreaName(),
            'cityname_uy' => $city->getAreaNameUy(),
            'adcode'      => $area->getAdCode(),
            'adname'      => $area->getAreaName(),
            'adname_uy'   => $area->getAreaNameUy(),
            'iscity'      => $iscity,
            'name'        => $iscity ? $city->getAreaName() : $area->getAreaName(),
            'name_uy'     => $iscity ? $city->getAreaNameUy() : $area->getAreaNameUy(),
            'station'     => $station ? (new StationResource($station)) : null,
            'airport'     => $airport ? (new LocationAirportResource($airport)) : null,
            'hotel'       => [

            ],
        ]);
    }


    public function provinces(Request $request)
    {
        return $this->success('', ProvincesResource::collection($this->areaRepo->listProvinces(false)));
    }
}
